package com.iculture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONException;

import com.iculture.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import android.widget.Toast;

public class FirstJsonExUrlActivity extends Activity {
	/** Called when the activity is first created. */
	// private Button getJsonbtn;
	// private Handler mHandler = new Handler();
	private ProgressDialog progDlg;
	private Dialog myDialog;
	private Button share;
	private Button match;
	private Button ticket;
	private Button favorite;
	private ImageView cancel;
	private TextView title;
	private TextView time;
	private TextView location;
	private TextView locationName;
	private TextView description;
	private ArrayList<Activity_DataStructure> DataList = new ArrayList<Activity_DataStructure>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setupViewComponent();
		loader();

	}

	private void setupViewComponent() {

		// getJsonbtn = (Button) findViewById(R.id.getJsonbtn);
		// getJsonbtn.setOnClickListener(getJsonbtnOnClick);
		// mBtnProgDlg.setOnClickListener(btnProgDlgOnClkLis);
	}

	/*
	 * private Button.OnClickListener getJsonbtnOnClick = new
	 * Button.OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { loader(); } };
	 */

	private void loader() {
		progDlg = new ProgressDialog(FirstJsonExUrlActivity.this);
		progDlg.setMessage(getString(R.string.loadingtxt));
		progDlg.setIcon(android.R.drawable.ic_dialog_info);
		progDlg.setCancelable(false);
		progDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDlg.setMax(100);
		progDlg.show();
		new Thread(new Runnable() {
			public void run() {
				executeAsyncTask();

			}
		}).start();
	}

	private class GetDataAyncTask extends
			AsyncTask<Hashtable<String, String>, Void, String> {

		@Override
		protected String doInBackground(Hashtable<String, String>... params) {
			Hashtable ht = params[0];

			String json = HelperHttp.getJSONResponseFromURL("output.php", ht);
			if (json != null) {
				Activity_DataStructure data = new Activity_DataStructure();
				DataList = data.parseJsonString(json);
				progDlg.setProgress(100);
				progDlg.cancel();
			} else {
				return "Invalid Param";
			}
			return "SUCCESS";
		}

		@Override
		protected void onPostExecute(String result) {

			if (result == "SUCCESS") {
				Toast.makeText(FirstJsonExUrlActivity.this, "Success",
						Toast.LENGTH_SHORT).show();
				final DataArrayAdapter adapter = new DataArrayAdapter(
						FirstJsonExUrlActivity.this, R.id.title, DataList);
				ListView listv = (ListView) findViewById(R.id.lv);
				listv.setAdapter(adapter);
				listv.setScrollingCacheEnabled(false);// 解決殘影問題
				listv.setDivider(new ColorDrawable()); // listview間隔顏色
				listv.setDividerHeight(15); // listview間距
				listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView parent, View view,
							final int position, long id) {
						/*
						 * Toast.makeText(FirstJsonExUrlActivity.this,
						 * adapter.getItem(position).title,
						 * Toast.LENGTH_SHORT).show();
						 */
						myDialog = new Dialog(FirstJsonExUrlActivity.this,
								R.style.PauseDialog);
						myDialog.setContentView(R.layout.activity_dialog_layout);
						myDialog.setTitle(R.string.activity_detail);	
						title = (TextView) myDialog
								.findViewById(R.id.dialog_title);
						time = (TextView) myDialog
								.findViewById(R.id.dialog_time);
						location = (TextView) myDialog
								.findViewById(R.id.dialog_location);
						locationName = (TextView) myDialog
								.findViewById(R.id.dialog_locationName);
						description = (TextView) myDialog
								.findViewById(R.id.dialog_description);
						share = (BgButton) myDialog
								.findViewById(R.id.btn_share);
						match = (BgButton) myDialog
								.findViewById(R.id.btn_match);
						ticket = (BgButton) myDialog
								.findViewById(R.id.btn_ticket);
						favorite = (BgButton) myDialog
								.findViewById(R.id.btn_love);
						cancel=(ImageView) myDialog.findViewById(R.id.dialog_cancel);

						title.setText("活動名稱:\n"
								+ adapter.getItem(position).title + "\n");
						time.setText("活動時間:\n" + adapter.getItem(position).startEndTime
								+ "\n");
						locationName.setText("活動地點:\n"
								+ adapter.getItem(position).locationName + "\n");
						location.setText("活動地址:\n"
								+ adapter.getItem(position).location + "\n");
						if (adapter.getItem(position).showUnit.length() > 2) {
							location.append("\n表演單位:\n"
									+ adapter.getItem(position).showUnit + "\n");
						}

						description.setText("活動說明:\n"
								+ adapter.getItem(position).descriptionFilterHtml
								+ "\n");

						if (!adapter.getItem(position).webSales
								.contains("http")) {
							ticket.setText("無售票");
							ticket.setEnabled(false);
						}
						
						cancel.setOnClickListener(new ImageView.OnClickListener(){
							public void onClick(View v) {
								myDialog.cancel();
							}
						});

						Window dialogWindow = myDialog.getWindow();
						WindowManager m = dialogWindow.getWindowManager();
						Display d = m.getDefaultDisplay();
						LayoutParams p = dialogWindow.getAttributes();
						// p.height = (int) (d.getHeight() * 1);
						p.width = (int) (d.getWidth() * 1);
						p.alpha = 1.0f;
						dialogWindow.setAttributes(p);

						ticket.setOnClickListener(new Button.OnClickListener() {
							public void onClick(View v) {
								Uri uri = Uri.parse(adapter.getItem(position).webSales);
								final Intent it = new Intent(
										Intent.ACTION_VIEW, uri);
								startActivity(it);

							}
						});

						myDialog.setCanceledOnTouchOutside(true);
						myDialog.show();
					}
				});

			} else {
			}
		}

	}

	private void executeAsyncTask() {
		Hashtable<String, String> ht = new Hashtable<String, String>();
		GetDataAyncTask async = new GetDataAyncTask();
		ht.put("type", "5");// 設定json url搜尋參數
		// ht.put("category", "2");
		Hashtable[] ht_array = { ht };
		async.execute(ht_array);
	}
}